package com.dsteiner.xelavos.client.controller;

import java.util.List;
import java.util.Map;

import com.dsteiner.widgets.client.SuggestBoxWidget.Suggestion;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.IFlowPanel;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.event.EventManager;
import com.dsteiner.widgets.client.rpc.SearchRequest;
import com.dsteiner.widgets.client.rpc.SearchResult;
import com.dsteiner.xelavos.client.SearchInfoWidget.SearchInfoModel;
import com.dsteiner.xelavos.client.domain.SearchInfo;
import com.dsteiner.xelavos.client.event.SearchEvent;
import com.dsteiner.xelavos.client.event.SearchInfoEvent;
import com.dsteiner.xelavos.client.event.SearchResultEvent;
import com.dsteiner.xelavos.client.event.TabSelectionEvent;
import com.dsteiner.xelavos.client.model.MainTabModel;
import com.dsteiner.xelavos.client.model.MainTabModel.Tab;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

public class SearchInfoController {

	private SearchInfoModel searchInfoModel;
	private IFlowPanel tabsSearchContentPanel;
	private IHTML tabsSearchInfoHtml;
	private MainTabModel mainTabModel;
	private EventManager eventManager;
	private IWidget tabsSearchPanel;
	private IWidget suggestionsPanel;
	private IHTML suggestionsInfoHtml;
	private IFlowPanel suggestionsContentPanel;
	private IFlowPanel wordsSuggestionsPanel;


	public void registerEventManager(EventManager eventManager) {
		this.eventManager = eventManager;
	}

	public void registerMainTabModel(MainTabModel mainTabModel) {
		this.mainTabModel = mainTabModel;
	}

	public void registerSearchInfoModel(SearchInfoModel searchInfoModel) {
		this.searchInfoModel = searchInfoModel;
	}

	public void registerSearchInfoWidget(ILazyWidget searchInfoWidget) {
		searchInfoWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateTabsSearchPanel();
			}
		});
	}

	public void registerTabsSearchContentPanel(IFlowPanel tabsSearchContentPanel) {
		this.tabsSearchContentPanel = tabsSearchContentPanel;
	}

	public void registerTabsSearchInfoHtml(IHTML tabsSearchInfoHtml) {
		this.tabsSearchInfoHtml = tabsSearchInfoHtml;
	}

	public void registerTabsSearchPanel(IWidget tabsSearchPanel) {
		this.tabsSearchPanel = tabsSearchPanel;
	}

	private void updateTabsSearchPanel() {
		SearchInfoEvent searchInfoEvent = eventManager.getEvent(SearchInfoEvent.class);
		SearchInfo searchInfo = searchInfoEvent != null ? searchInfoEvent.getSearchInfo() : null;
		if(searchInfo == null)
			return;
		
		SearchEvent event = eventManager.getEvent(SearchEvent.class);
		final SearchRequest searchRequest = event.getSearchRequest();
		String searchText = searchRequest.getText();
		Map<Tab, SearchResult<ModelObject>> tabSearchResults = searchInfo.getTabSearchResults();
		tabsSearchPanel.setVisible(!tabSearchResults.isEmpty());
		tabsSearchInfoHtml.setHTML("Buscó en el area equivocado? intente :");
		tabsSearchContentPanel.clear();
		for (final Tab tab : tabSearchResults.keySet()) {
			final SearchResult<ModelObject> searchResult = tabSearchResults.get(tab);
			HTMLExt searchLink = new HTMLExt("<a href=\"javascript:void(0);\">"+
					(searchText != null ? searchText+" en " : "")
					+tab.getName()+" con "+searchResult.getCount()+" resultados</a>");
			searchLink.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent arg0) {
					eventManager.fireEvent(new TabSelectionEvent(tab));
					eventManager.fireEvent(new SearchResultEvent(searchResult));
				}
			});
			tabsSearchContentPanel.add((Widget)searchLink);
		}
		
		List<Suggestion> suggestions = searchInfo.getStartWithSuggestions();
		suggestionsPanel.setVisible(!suggestions.isEmpty());
		suggestionsInfoHtml.setHTML("El principio de la palabra debe coincidir con los resultados - intente :");
		suggestionsContentPanel.clear();
		for (final Suggestion suggestion : suggestions) {
			HTMLExt suggestionLink = new HTMLExt("<a href=\"javascript:void(0);\">"+suggestion.getDisplayText()+"</a>");
			suggestionLink.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent arg0) {
					searchRequest.setText(suggestion.getDisplayText());
					eventManager.fireEvent(new SearchEvent(searchRequest));
				}
			});
			suggestionsContentPanel.add((Widget)suggestionLink);
		}
		
		List<Suggestion> wordSuggestions = searchInfo.getWordSuggestions();
		wordsSuggestionsPanel.setVisible(!wordSuggestions.isEmpty());
		wordsSuggestionsPanel.clear();
		wordsSuggestionsPanel.add(new HTML("Palabras de su busqueda que tienen resultados : "));
		for (final Suggestion suggestion : wordSuggestions) {
			HTMLExt suggestionLink = new HTMLExt("<a href=\"javascript:void(0);\">"+suggestion.getDisplayText()+"</a>");
			suggestionLink.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent arg0) {
					searchRequest.setText(suggestion.getDisplayText());
					eventManager.fireEvent(new SearchEvent(searchRequest));
				}
			});
			wordsSuggestionsPanel.add((Widget)suggestionLink);
		}
	}

	public void registerSuggestionsPanel(IWidget suggestionsPanel) {
		this.suggestionsPanel = suggestionsPanel;
	}

	public void registerSuggestionsInfoHtml(IHTML suggestionsInfoHtml) {
		this.suggestionsInfoHtml = suggestionsInfoHtml;
	}

	public void registerSuggestionsContentPanel(IFlowPanel suggestionsContentPanel) {
		this.suggestionsContentPanel = suggestionsContentPanel;
	}

	public void registerWordSuggestionsPanel(IFlowPanel wordsSuggestionsPanel) {
		this.wordsSuggestionsPanel = wordsSuggestionsPanel;
	}
}